// pages/cateringDishes/cateringDishes.js

const { extend, Field, Toast, Dialog } = require('../../zanui/index')
import { obj2Form } from '../../utils/util.js'

Page(extend({}, Field, Toast, Dialog, {

  /**
   * 页面的初始数据
   */
  data: {
    cateringId: null,
    dishesList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: options.storeName + '推荐菜'
    })
    this.setData({
      cateringId: options.id
    })
  },
  onShow: function () {
    if (this.data.cateringId) {
      this.getRecommendDishes()
    }
  },
  confirmCommit () {
    wx.navigateBack({
      
    })
  },
  toAddItem () {
    wx.navigateTo({
      url: `../cateringDishesEdit/cateringDishesEdit?cateringId=${this.data.cateringId}`,
    })
  },
  getRecommendDishes () {
    const _self = this
    wx.request({
      url: 'https://qdndc.ideatower.cn/v1/recommendedDishes',
      data: {
        belongCateringId: this.data.cateringId
      },
      success: (res) => {
        if (res.statusCode == 200 && res.data.code == '000000') {
          const data = res.data.data
          this.setData({
            dishesList: data
          })
        } else {
          this.showZanToast('获取数据失败，请返回重新获取')
        }
      },
      fail: (err) => {
        this.showZanToast('获取数据失败，请返回重新获取')
      }
    })
  },
  toDeleteItem(e) {
    const item = e.currentTarget.dataset.item
    const _self = this
    this.showZanDialog({
      content: `确认删除【${item.dishesName}】吗？`,
      showCancel: true
    }).then(() => {
      wx.request({
        url: 'https://qdndc.ideatower.cn/v1/recommendedDishes/' + item.id,
        method: 'DELETE',
        success: (res) => {
          if (res.statusCode == 200 && res.data.code == '000000') {
            _self.getRecommendDishes()
          } else {
            this.showZanToast('获取数据失败，请返回重新获取')
          }
        },
        fail: (err) => {
          this.showZanToast('获取数据失败，请返回重新获取')
        }
      })
    }).catch(() => {
    })
  },
  toUpdateItem(e) {
    console.log(e)
    const item = e.currentTarget.dataset.item
    wx.navigateTo({
      url: `../cateringDishesEdit/cateringDishesEdit?cateringId=${this.data.cateringId}&dishesId=${item.id}`,
    })
  }
}))